package com.qyhl.webtv.module_news.news.goodlife.list;


import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.qyhl.webtv.module_news.common.NewsUrl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by qyhl on 2017/11/27.
 */

public class GoodLifeListModel implements GoodLifeListContract.GoodLifeListModel {
    private GoodLifeListPresenter mPresenter;

    public GoodLifeListModel(GoodLifeListPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 获取置顶个数
     *
     * @param id
     */
    @Override
    public void getTopNum(String id) {
        String url = CommonUtils.getInstance().getCmsUrl();

        EasyHttp.get(url)
                .params("method", "queryTopArticle")
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("catalogID", id)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.topNumError();
                    }

                    @Override
                    public void onSuccess(String s) {
                        try {
                            JSONObject object = new JSONObject(s);
                            int num = Integer.valueOf(object.optString("returnMeg"));
                            mPresenter.setTopNum(num);
                        } catch (JSONException e) {
                            e.printStackTrace();
                            mPresenter.topNumError();
                        }
                    }
                });
    }

    /**
     * 获取新趣汇子栏目数据
     *
     * @param id     栏目ID
     * @param lastId 最后一条id
     */
    @Override
    public void getData(String id, final String lastId) {

        EasyHttp.get(NewsUrl.GET_HOME_NEWS)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("InnerCode", id)
                .params("ID", lastId)
                .execute(new SimpleCallBack<List<NewsBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            if ("0".equals(lastId)) {
                                mPresenter.setError(2, "暂无任何内容！");
                            } else {
                                mPresenter.setError(3, "no more");
                            }
                        } else {
                            if ("0".equals(lastId)) {
                                mPresenter.setError(4, "数据加载失败，稍后请重新尝试！");
                            } else {
                                mPresenter.setError(5, "数据加载失败，稍后请重新尝试！");
                            }
                        }
                    }

                    @Override
                    public void onSuccess(List<NewsBean> newsBeans) {
                        if (newsBeans != null && newsBeans.size() > 0) {
                            if ("0".equals(lastId)) {
                                mPresenter.setData(newsBeans, false);
                            } else {
                                mPresenter.setData(newsBeans, true);
                            }
                        } else {
                            if ("0".equals(lastId)) {
                                mPresenter.setError(2, "暂无任何内容！");
                            } else {
                                mPresenter.setError(3, "no more or no datas");
                            }
                        }
                    }
                });


    }


}
